package com.javaPro.myProject.modules.courseInfo.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.courseInfo.entity.CourseInfo;
import com.javaPro.myProject.modules.courseInfo.service.CourseInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 景点/特色信息(CourseInfo)表控制层
 */
@RestController
@RequestMapping("courseInfo")
public class CourseInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseInfoService courseInfoService;
@Resource
private uploadUtil uploadUtil;
    /**
     * 分页查询
     *
     * @param courseInfo 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(CourseInfo courseInfo) {
        startPage();
        return getList(this.courseInfoService.queryByPage(courseInfo));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public AjaxResult queryById(CourseInfo courseInfo) {
        return AjaxResult.success(this.courseInfoService.queryById(courseInfo));
    }

    /**
     * 新增数据
     *
     * @param courseInfo 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(CourseInfo courseInfo, MultipartFile file) {
        if (file != null){
            String upload = uploadUtil.upload(file);
            courseInfo.setPic(upload);
        }

        return AjaxResult.success(this.courseInfoService.insert(courseInfo));
    }

    /**
     * 编辑数据
     *
     * @param courseInfo 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public AjaxResult edit(CourseInfo courseInfo, MultipartFile file) {
        if (file != null){
            String upload = uploadUtil.upload(file);
            courseInfo.setPic(upload);
        }
        return AjaxResult.success(this.courseInfoService.update(courseInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("del")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.courseInfoService.deleteById(id));
    }

}

